import type { AxiosRequestConfig, AxiosResponse, AxiosError, AxiosPromise } from 'axios'
import { ElMessage } from 'element-plus'
import type { Result } from '@/utils/interface'
import store from 'store/index'
export const requestInterceptors =
  (requestHook: Function) =>
  (config: AxiosRequestConfig): AxiosRequestConfig => {
    // console.log(config)
    requestHook && requestHook(config)
    return config
  }

export const requestInterceptorsCatch =
  (requestCatchHook: Function) =>
  (error: AxiosError): AxiosPromise => {
    requestCatchHook && requestCatchHook(error)
    return Promise.reject(error)
  }

export const responseInterceptors =
  (responseHook: Function, responseCallback: Function) =>
  (response: AxiosResponse): AxiosPromise => {
    responseCallback && responseCallback(response)
    const res: Result = response.data
    if (response.config.responseType == 'blob') {
      return Promise.resolve(response)
    } else {
      if (res === void 0 || res === null || !res) {
        const message: string = `Error：${response.config.url} response.data is null or does not exist ！`
        console.log(response)
        ElMessage.error(message)
        return Promise.reject(message)
      } else if (res.code == 1 || res.code === 2000 || res.code === 20000 || res.code === 'success') {
        // 正确响应
        res.data === null && console.log(response)
        if (store.state.app.requestCount.count > 0 && Date.now() - store.state.app.requestCount.time > 1800000) {
          // 登录过期
          store.state.app.requestCount.count = 0
          store.state.app.requestCount.time = 0
        }

        return Promise.resolve(response)
        // responseHook && responseHook(response)
      } else if (res.code == 5001) {
        store.commit('app/increment')
        console.log(store.state.app.requestCount)

        if (store.state.app.requestCount.count <= 1) {
          // 登录过期
          responseHook && responseHook(response)
        }
        // return Promise.reject(res.code)
      } else {
        ElMessage.error(res.msg)
        return Promise.reject(res)
      }
    }
  }

export const responseInterceptorsCatch =
  (responseCatchHook: Function, responseCallback: Function) =>
  (error: AxiosError): AxiosPromise => {
    responseCallback && responseCallback(error)
    console.log(error, 777)

    if (error && error.response) {
      switch (error.response.status) {
        case 400:
          error.message = '请求错误(400)'
          break
        case 401:
          error.message = '未授权，请重新登录(401)'
          break
        case 403:
          if (error.response.data && error.response.data.code === 5001) {
            error.message = error.response.data.msg
          } else {
            error.message = '拒绝访问(403)'
          }
          break
        case 404:
          error.message = '请求出错(404)'
          break
        case 408:
          error.message = '请求超时(408)'
          break
        case 500:
          error.message = '服务器错误(500)'
          break
        case 501:
          error.message = '服务未实现(501)'
          break
        case 502:
          error.message = '网络错误(502)'
          break
        case 503:
          error.message = '服务不可用(503)'
          break
        case 504:
          error.message = '网络超时(504)'
          break
        case 505:
          error.message = 'HTTP版本不受支持(505)'
          break
        default:
          error.message = `连接出错(${error.response.status})!`
      }
    }

    if (error.message && error.message.indexOf('timeout') !== -1) {
      error.message = '请求超时'
    }

    if (error.response?.data && error.response.data.code === 5001) {
      // 过期
      return responseCatchHook && responseCatchHook(error)
    }

    error.message && ElMessage.error(error.message)

    return Promise.reject({ ...error.response })
  }
